<?php
//load config
include "../includes/config/config.php";
//create object db
include "../includes/class_mysql.php";
include "../includes/gpc_function.php";
//include ("../includes/phpmailer/function.sendmail.php");
include('includes/functions.php');
include('includes/global.php');


$dbsql->Backup();exit;

checkPromotionExdate();
SendNewsLetter();

$msg    = 'test cronjob at: '.date('Y-m-d: H:i:s');
//send_email('trinhpn@traviscon.com','trinh','cronjob',$msg);

function checkPromotionExdate()
{
    global $dbsql,$language;
    $datenow    = date('Y-m-d');
    $sql_delete = "delete from tblpromotions where dEndDate < '$datenow' and dEndDate!='0000-00-00' ";
    $dbsql->query($sql_delete);
    echo 'checkpromotion';
}

function SendNewsLetter()
{
    global $dbsql,$language,$root_domain;
    
    $month      = date("m");
    $year       = date("Y");
    //select content newsletter:
    $sql_select = "select cTitle,cContent from tblnewsletter where iMonth=$month and iYear = $year and iStatus=1";
    $rs=$dbsql->query($sql_select);
    if( $result = $dbsql->fetch_array($rs) )
    {
    	$subject= $result['cTitle'];
    	$message= $result['cContent'];
    	
    	if(empty($subject))$subject='News Letter from '.$root_domain;
    	
    	$message= str_replace(array("\\\"","\\'","\\\""),array("","'",'"'),$message);
    	$message= str_replace("../",$root_domain."/",$message);	
    	
    	//send newletter:
    	$sql_select="select cEmail,cName from tblnewsletter_email where iStatus='1' and iMonth!= ".$month." limit 10";
    	
    	$rs=$dbsql->query($sql_select);
    	while ( $result = $dbsql->fetch_array($rs) )
    	{
    			$to_email=$result["cEmail"];	
    			$linkunsubscribe='<a href="'.$root_domain."/as/unsubscribe/".$result['cName'].'".htm">UNSUBSCRIBE</a>';
    			$messagesend=str_replace("[UNSUBSCRIBE]",$linkunsubscribe,$message);
    			send_email($to_email, "",$subject,$messagesend);
    			//update month for email:
    			
    			$sql_update="update tblnewsletter_email set iMonth='".$month."' where cEmail='".$result['cEmail']."'";
    			$dbsql->query($sql_update);
    	}
    }
}
?>